﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace WW.NActiveRecord.Internal.Utils
{
    internal class CollectionUtil 
    {
        
        /// <summary>
        /// ToHash(true,a=>"aa",b=>"bb")  // ->  {"a":"aa","b":"bb"}
        /// </summary>
        /// <param name="pairs"></param>
        /// <returns></returns>
        public static IDictionary<string,TValue> ToHash<TValue>(params Func<string,TValue>[] pairs) 
        {
            IDictionary<string, TValue> result = new Dictionary<string, TValue>();
            foreach (var pair in pairs) {
                string parameter = pair.Method.GetParameters()[0].Name;
                TValue value = pair(parameter);
                result.Add(parameter, value);
            }
            return result;
        }

        /// <summary>
        /// ToHashParameterAsKey(true,a=>"aa",b=>"bb")  // ->  {"aa":"a","bb":"b"}
        /// </summary>
        /// <param name="pairs"></param>
        /// <returns></returns>
        public static IDictionary<string, string> ToHashParameterAsKey(params Func<string, string>[] pairs) {
            IDictionary<string, string> result = new Dictionary<string, string>();
            foreach (var pair in pairs) {
                string parameter = pair.Method.GetParameters()[0].Name;
                string value = pair(parameter);
                result.Add(value, parameter);
            }
            return result;
        }
    }
}